package com.cskaoyan.controller;

import com.cskaoyan.bean.Admin;
import com.cskaoyan.bean.Role;
import com.cskaoyan.bo.*;
import com.cskaoyan.bean.Storage;
import com.cskaoyan.service.SystemManagementService;
import com.cskaoyan.service.UserManagementService;
import com.cskaoyan.vo.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin")
@RequiresRoles("超级管理员")
public class SystemManagementController {
    @Autowired
    SystemManagementService systemManagementService;

    @GetMapping("role/options")
    public BaseRespVo roleOptions(){
        List<RoleVO> roleVOS = systemManagementService.selectRole();
        return BaseRespVo.ok(roleVOS);
    }

    @GetMapping("admin/list")
    public BaseRespVo adminList(AdminBO adminBO){
        AdminVO adminVO = systemManagementService.query(adminBO);
        return BaseRespVo.ok(adminVO);
    }

    @PostMapping("admin/create")
    public BaseRespVo adminCreate(@RequestBody @Validated Admin admin){
        Admin admin1 = systemManagementService.addAdmin(admin);
        if (admin1 != null) {
            return BaseRespVo.ok(admin1);
        }else {
            return BaseRespVo.fail("管理员已存在",402);
        }
    }

    @PostMapping("admin/delete")
    public BaseRespVo adminDelete(@RequestBody Admin admin){
        systemManagementService.deleteAdmin(admin);
        return BaseRespVo.ok();
    }

    @PostMapping("admin/update")
    public BaseRespVo adminUpdate(@RequestBody @Validated Admin admin){
        systemManagementService.updateAdmin(admin);
        return BaseRespVo.ok();
    }

    @GetMapping("log/list")
    public BaseRespVo logList(LogBO logBO){
        LogVO logVO = systemManagementService.queryLog(logBO);
        return BaseRespVo.ok(logVO);
    }

    @GetMapping("role/list")
    public BaseRespVo getRole(RoleBO roleBO){
        RoleGetVO roleGetVO = systemManagementService.getRole(roleBO);
        return BaseRespVo.ok(roleGetVO);
    }

    @PostMapping("role/create")
    public BaseRespVo createRole(@RequestBody Map<String, Object> roleMap){
        RoleCreateVO role = systemManagementService.createRole(roleMap);
        return BaseRespVo.ok(role);
    }

    // op:delete和update操作
    @PostMapping("role/{op}")
    public BaseRespVo updateRole(@RequestBody Role role, @PathVariable("op")String op){
        systemManagementService.updateRole(role, op);
        return BaseRespVo.ok();
    }

    @GetMapping("storage/list")
    public BaseRespVo updateRole(StorageBO storageBO){
        StorageVOGet storageVOGet = systemManagementService. getStorageList(storageBO);
        return BaseRespVo.ok(storageVOGet);
    }

    @PostMapping("storage/{op}")
    public BaseRespVo updateStorage(@RequestBody Storage storage, @PathVariable("op")String op){
        systemManagementService.updateStorage(storage, op);
        return BaseRespVo.ok();
    }

    @GetMapping("role/permissions")
    public BaseRespVo permissions(int roleId) {
        PermissionsVO vo = systemManagementService.getPermissions(roleId);

        return BaseRespVo.ok(vo);
    }

    @PostMapping("role/permissions")
    public BaseRespVo permissions(@RequestBody PermissionsBO permissionsBO) {

        if (permissionsBO.getRoleId() == 1) {
            return BaseRespVo.fail("当前角色的超级权限不能变更", 641);
        }

        int count = systemManagementService.updatePermisssions(permissionsBO);
        return BaseRespVo.ok();
    }
}
